#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import logging
import os

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False


logfile_dir = "./logs"
logfile_name = "app.log"
if not os.path.isdir(logfile_dir):
    os.mkdir(logfile_dir)

logfile_path = os.path.join(logfile_dir, logfile_name)
file_handler = logging.FileHandler(logfile_path, mode="w")
file_handler.setLevel(logging.DEBUG)

stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)

formatter = logging.Formatter(
    "%(asctime)s - %(name)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s"
)
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)

# ✅ 只添加一次，避免重复输出
if not logger.handlers:
    logger.addHandler(file_handler)
    logger.addHandler(stream_handler)
